<?php
@session_start();

//岁末大换购活动插件
//By avenger
//2006-11-28 21:58
//$Id: 2006ye.php 9 2009-04-26 06:35:12Z bole.Chen $

//满 50 元，对以下产品做换购
//换购要求最低金额
$ye2006['start'] = 68;

//一张订单换购最大物品数
//Todo
$ye2006['max'] = 200;

//换购产品清单
/*产品ID=>原价,换购价*/
$ye2006['item'] = array(
    202     => array(38, 26), //深绿水塞
    381     => array(26, 18), //环保猪手电
    283     => array(125, 88), //T.DOG狗狗手腕保护垫(黑)
    284     => array(125, 88), //T.DOG狗狗手腕保护垫(褐)
    640     => array(125, 88), //T.DOG狗狗手腕保护垫(土黄)

    359     => array(58, 38), //茶犬(红)
    361     => array(58, 38), //茶犬(黑)
    208     => array(138, 98), //笑脸杯
    303     => array(22, 14), //不锈钢皂

    297     => array(12, 4), //茶匙(绿)
    180     => array(12, 4), //茶匙(黄)
    296     => array(12, 4), //茶匙(白)
    238     => array(66, 38), //笑福犬
    217     => array(68, 38), //十二色灯

    196     => array(18, 9), //心型蛋锅
    63      => array(58, 42), //狗狗钥匙圈(黑)
    62      => array(58, 42), //狗狗钥匙圈(白)
    64      => array(58, 42), //狗狗钥匙圈(灰)
    65      => array(58, 42), //狗狗钥匙圈(黄)
    289     => array(88, 68), //狗骨头手腕垫(黄)
    290     => array(88, 68), //狗骨头手腕垫(绿)
    288     => array(88, 68), //狗骨头手腕垫(白)
    233     => array(138, 98), //黑钛手链

    243     => array(118, 88), //黑钛皮绳
    407     => array(118, 98), //果汁闹钟
    166     => array(168, 128), //小鸡计算器

    //动漫区
    139 => array(60, 53), //青蛙军曹五只小公仔大集合
    147 => array(18, 12), //盒装金属手里剑-火影周边
    148 => array(25, 16), //搪胶盒装小苦无-火影周边
    127 => array(20, 14), //one piece娜美布袋毛绒公仔
    128 => array(20, 14), //one piece山治布袋毛绒公仔
    131 => array(40, 29), //网球王子龙马毛绒变身熊公仔
    306 => array(28, 18), //one piece海盗头巾
    315 => array(21, 15), //死神Bleach圆吊牌多功能挂扣
    316 => array(40, 30), //猎人-库拉皮卡第一代手链
    317 => array(15, 9), //one piece黑色运动头带
    311 => array(36, 28), //青蛙军曹小挂件及胸针套装
    118 => array(30, 20), //高达SEED哈罗毛绒小公仔(黄)
    117 => array(30, 20), //高达SEED哈罗毛绒小公仔(桃红)
    116 => array(30, 20), //高达SEED哈罗毛绒小公仔(黑)
    309 => array(18, 9), //死神Bleach圆筒笔袋
    305 => array(25, 20), //高达SEED哈罗彩色手链
    310 => array(20, 16), //青蛙军曹彩色手链
    304 => array(19, 10), //吉祥寺咖啡屋卡通帆布笔袋
    308 => array(15, 9), //棋魂双面笔袋
    146 => array(34, 28), //海盗船(外包装盒瑕疵)

    519 => array(14, 12), //佐助万圣节胸针-火影周边
    520 => array(14, 12), //鸣人万圣节胸针-火影周边
    521 => array(14, 12), //卡卡西万圣节胸针-火影周边
    522 => array(26, 21), //六款人物钥匙扣套装-火影周边
    523 => array(40, 33), //one piece-8款人气钥匙扣套装
    524 => array(40, 32), //犬夜叉7款人物钥匙扣套装
    525 => array(14, 11), //火影NARUTO标志手链
    526 => array(8, 6), //火影手机绳-小樱
    564 => array(40, 32), //犬夜叉7款人物钥匙扣套装

    //07夏
    175 => array(155,128), //爱的玻璃杯组-LOVE PACK
    409 => array(198,168), //西部牛仔开瓶器
    332 => array(126,98), //个性化透明挂表
    538 => array(88,58), //清凉动物制冰盒-小鱼
    537 => array(88,58), //清凉动物制冰盒-企鹅
    539 => array(88,58), //清凉动物制冰盒-骨头
    87 => array(64,46), //鲨鱼开瓶器(绿)
    88 => array(64,46), //鲨鱼开瓶器(红)
    244 => array(39,18), //笑脸水果签
    347 => array(58, 46), //企鹅饮水机
    168 => array(160, 110), //水汪汪杯组
    201 => array(38, 26), //粉红水塞
    199 => array(38, 26), //草绿水塞
    200 => array(38, 26), //黄水塞

);


add_action('shopcar_shopCarList_Befor', 'ye2006_getlist_befor');
add_action('view_items_price_after', 'ye2006_view_items_price');
//add_action('view_shopcarlist_after', 'ye2006_view_shopcarlist_after');
add_action('view_whatnew_item', 'ye2006_view_bigprice');
add_action('view_whathot_item', 'ye2006_view_bigprice');
add_action('view_itemslist_item', 'ye2006_view_bigprice');

//店休
if ((time() < strtotime('2007-10-07')) && !$_SESSION['view_checkout_msg_10_1']) add_action('view_checkout_msg', 'view_checkout_msg');

function view_checkout_msg() {
    $_SESSION['view_checkout_msg_10_1'] = true;
    echo "<script type='text/javascript'>addDOMLoadEvent(function(){alert('乐铺提醒：10 月 1-6 日乐铺国庆店休，期间订单会延迟到 7 日后处理。')});</script>";
    echo '<div class="errbox roundNormal" style="width:80%">&nbsp;&nbsp;&nbsp;乐铺提醒：1-6 日乐铺国庆节店休，期间付款订单会延迟到 7 日通过免费快递发出。<a class="white" href="/blog/?p=499" target="_blank">查看详情</a></div><br clear="all" />';
}

$ye2006_display_ad = false;

//Test 引用变量
//add_action('shopcar_shopCarList', 'shopcar_shopCarList');
function shopcar_shopCarList(&$result) {
    $result['list'][0]['iname'] = 'shopcar_shopCarList';
}

//获取购物车数据
function ye2006_getlist_befor($sid) {
    global $ye2006, $ye2006_display_ad;

    $db = getDB();

    //折前总金额
    //$total = $db->GetOne(getTable('shopcar'), 'SUM(amount*price)', "sessionid=".$db->qstr($sid));
    //if ($total < $ye2006['start']) return;

    $data = $db->GetAll(getTable('shopcar'), '*', "sessionid=".$db->qstr($sid));

    //折后总金额
    foreach ($data as $v) {
        if ( array_key_exists($v['iid'], $ye2006['item']) ) {
            $total += $ye2006['item'][$v['iid']][1]*$v['amount'];
        } else {
            $total += $v['price']*$v['amount'];
        }
    }

    foreach ($data as $v) if ( array_key_exists($v['iid'], $ye2006['item']) ) {

        //已折扣
        if ($v['price'] == $ye2006['item'][$v['iid']][1]) {
            //物品数统计
            $i_i += $v['amount'];
            if ($i_i > $ye2006['max']) return;
        }

        $v['iname'] = preg_replace('/\(换购,原价[^\)]+\)$/', '', $v['iname']);

        if ( ($total < $ye2006['start']) && $i_i) {
            //条件不满足时更新为初始价
            $fields['iname']    = preg_replace('/\(换购,原价[^\)]+\)$/', '', $v['iname']);
            $fields['price']    = $ye2006['item'][$v['iid']][0];
            $db->Update(getTable('shopcar'), $fields, "id=".$v['id']);
        } else if ( ($total >= $ye2006['start']) && ($v['price'] > $ye2006['item'][$v['iid']][1]) ) {
            $ye2006_display_ad = false;
            //满足条件时更新为换购价
            $fields['iname']    = $v['iname'].'(换购,原价'.$v['price'].')';
            $fields['price']    = $ye2006['item'][$v['iid']][1];
            $db->Update(getTable('shopcar'), $fields, "id=".$v['id']);
        }

    }

}

//物品展示页
function ye2006_view_items_price($item) {
    global $ye2006;

    if ( array_key_exists($item['id'], $ye2006['item']) ) {
        //echo "&nbsp;&nbsp;<font class=\"price_small\">清凉价: &yen;".$ye2006['item'][$item['id']][1]."</font> - <font class='px11'>(<a href='javascript:void(0);' onclick='javascript:alert(\"清凉一夏换购活动说明:\\n--------------------------------\\n1、购物总金额满 68 元，即可以清凉价换购指定物品；\\n2、本活动不能与其它优惠活动同时享有。\")'>这是什么?</a>)</font>";
        echo "&nbsp;&nbsp;<font class=\"price_small\">清凉价: &yen;".$ye2006['item'][$item['id']][1]."</font> - <font class='px11'>(<a href='http://www.loopo.cn/events/2007summer.php' target='_blank'>活动说明</a>)</font>";
    }
}

//列表页广告
function ye2006_view_shopcarlist_after() {
    global $ye2006_display_ad;

    if ($ye2006_display_ad == true) echo "<br /><br /><h2>【小广告：购满50元，20种商品5折起，点击图片看详情】</h2><center><a href='/events/2006ye.php'><img src='/images/index/1year.jpg' alt='酷夏清凉价，二十种商品五折起' /></a></center>";
}

//列表页价格 - 大
function ye2006_view_bigprice(&$item) {
    global $ye2006;

    if (array_key_exists($item['id'], $ye2006['item'])) {
        $item['price1'] = $ye2006['item'][$item['id']][1].'.- (清凉价)';
    }
}
?>
